var express = require('express');
var axios = require('axios');
var router = express.Router();

/**
 * @api {get} /admin/createExam 渲染页面
 * @apiName 渲染页面
 * @apiGroup createExam
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    // res.send("登陆页面");
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Teacher/getAllKind',
        params: {
            Kind: 'b'
        },
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("获取教师信息成功")
                res.render('admin/createExam', {
                    list: response.data.data
                });
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取教师信息失败" })
                console.log("获取教师信息失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误，请刷新页面" })
            console.log("服务器错误")
        });
})

/**
 * @api {post} /admin/createExam/buildTest 发布一个试卷创建任务
 * @apiName 发布一个试卷创建任务
 * @apiGroup createExam
 *
 * @apiSuccessExample Request:
 * {
 *  "tbExamWorks": [
 *    {
 *      "endTime": "string",
 *      "id": 0,
 *      "note": "string",
 *      "startTime": "string",
 *      "status": 0,
 *      "teacherId": 0,
 *      "testWordId": "string",
 *      "workProblemData": "string"
 *    }
 *  ],
 *  "tbTestWord": {
 *    "id": 0,
 *    "status": 0,
 *    "subject": "string",
 *    "testAmount": 0,
 *    "testAmountGet": 0,
 *    "testIntroduce": "string",
 *    "testName": "string",
 *    "testNumber": "string"
 *  }
 *}
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/buildTest',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, (req, res, next) => {
    let { tbExamWorks, tbTestWord } = req.body
    console.log(req.body)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/TestWord/buildTest',
        data: req.body
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("创建试卷成功")
                res.send({ err: 0, msg: "创建试卷成功", data: response.data })
            } else {
                res.send({ err: -1, msg: data.msg })
                console.log("创建试卷失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("服务器错误")
        });
})

module.exports = router;
